package com.ra.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.googlecode.cswish.model.PageInfo;
import com.ra.util.baidu.BaiduApiUtils;
import com.ra.util.baidu.BaiduMarker;
import com.ra.util.baidu.BaiduPlaceDetail;

/**
 * Bai.du utility class
 * 
 * @author kang.shen 2015-04-07
 * 
 */
@Service
public class BaiduService {

	/**
	 * 通过关键字获取百度坐标半径范围内的对应点
	 * 
	 * @param longitude 经度
	 * @param latitude 纬度
	 * @param radius 半径
	 * @param pageNo 页码
	 * @param pageSize 页大小
	 * @param keyword 关键字
	 * @return
	 */
	public List<BaiduMarker> getMarkerArrsByKeyWord(double longitude, double latitude,
			double radius, int pageNo, int pageSize, String keyword) {
		List<BaiduMarker> markerArrs = new ArrayList<BaiduMarker>();
		PageInfo<BaiduPlaceDetail> pageInfo = BaiduApiUtils
				.searchPlaceByCoordinate(longitude, latitude, radius, pageNo,
						pageSize, keyword);
		if(pageInfo == null) {
			return null;
		}
		List<BaiduPlaceDetail> baiduPlaceDetails = pageInfo.getData();
		BaiduMarker baiduMarker = null;
		BaiduPlaceDetail baiduPlaceDetail = null;
		String title = null;
		String url = "";
		String point = null;
		String content = null;
		int isOpen = 0;
		if (CollectionUtils.isNotEmpty(baiduPlaceDetails)) {
			for (int i = 0; i < baiduPlaceDetails.size(); i++) {
				baiduMarker = new BaiduMarker();
				baiduPlaceDetail = baiduPlaceDetails.get(i);
				title = baiduPlaceDetail.getName();
				if(keyword.endsWith(RaConstant.BAIDU_TRANSIT_KEYWORD) || 
						keyword.endsWith(RaConstant.BAIDU_SUBWAY_KEYWORD)) { // 公交、地铁
					content = keyword + "：" + baiduPlaceDetail.getAddress();
				} else if (keyword.endsWith(RaConstant.BAIDU_HOTEL_KEYWORD) || 
						keyword.endsWith(RaConstant.BAIDU_RESTAURANT_KEYWORD) ||
						keyword.endsWith(RaConstant.BAIDU_HOSPITAL_KEYWORD) ||
						keyword.endsWith(RaConstant.BAIDU_SCHOOL_KEYWORD)) { // 酒店、餐饮、医院、学校
					content = "电话：" + baiduPlaceDetail.getTelephone() + 
							"<br>" + "地址：" + baiduPlaceDetail.getAddress();
				} else { // 超市、购物、银行
					content = "地址：" + baiduPlaceDetail.getAddress();
				}
				point = baiduPlaceDetail.getLongitude() + "|" + baiduPlaceDetail.getLatitude();

				baiduMarker.setTitle(title);
				baiduMarker.setUrl(url);
				baiduMarker.setContent(content);
				baiduMarker.setPoint(point);
				baiduMarker.setIsOpen(isOpen);
				
				markerArrs.add(baiduMarker);
			}
		}
		return markerArrs;
	}
}
